package constant.milk.periodapp.setting;

import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import constant.milk.periodapp.MainActivity;
import constant.milk.periodapp.R;
import constant.milk.periodapp.SengriCheck;
import constant.milk.periodapp.SuperActivity;
import constant.milk.periodapp.database.DbInsertQuery;
import constant.milk.periodapp.dialog.MilkCountDialog;
import constant.milk.periodapp.dialog.MilkCountDialog.CountCallBack;
import constant.milk.periodapp.dialog.MilkDayDialog;
import constant.milk.periodapp.dialog.MilkDayDialog.DateCallBank;
import constant.milk.periodapp.util.MilkShared;
import constant.milk.periodapp.util.MilkUtil;

public class SengriActivity extends SuperActivity
{
	public static final String SENGRI_FLAG = "SENGRI_FLAG";
	public static final int SETTING = 0;
	public static final int FIRST = 1;
	
	private TextView backTexTiew;
	private TextView titleTextView;
	
	private View dateView;
	private TextView dateTextView;
	
	private View termView;
	private TextView termTextView;
	
	private View firstView;
	private TextView firstTextView;
	
	private View averageView;
	private TextView averageTextView;
	
	private TextView startTextView;
	
	private int year;
	private int month;
	private int day;
	
	private int dateCount;
	private int termCount;
	private int sengriType;
	
	private int flag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Intent intent = getIntent();
		flag = intent.getIntExtra(SENGRI_FLAG, SETTING);
		
		if (flag == FIRST)
		{
			isAdFlag = false;
		}
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.sengri_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		backTexTiew = (TextView) findViewById(R.id.sengriBackTextView);
		titleTextView = (TextView) findViewById(R.id.sengriTitleTextview);
		
		dateView = findViewById(R.id.sengriDateView);
		dateTextView = (TextView) findViewById(R.id.sengriDateTextView);
		
		termView = findViewById(R.id.sengriTermView);
		termTextView = (TextView) findViewById(R.id.sengriTermTextView);
		
		firstView = findViewById(R.id.sengriFirstView);
		firstTextView = (TextView) findViewById(R.id.sengriFirstTextView);
		
		averageView = findViewById(R.id.sengriAverageView);
		averageTextView = (TextView) findViewById(R.id.sengriAverageTextView);
		
		startTextView = (TextView) findViewById(R.id.sengriStartTextView);
	}

	@Override
	protected void setOnClick()
	{
		backTexTiew.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		
		dateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkCountDialog dialog = new MilkCountDialog(superContext, "생리기간", 
						new CountCallBack()
				{
					@Override
					public void getCount(int count)
					{
						dateCount = count;
						dateTextView.setText(dateCount + "일");
						
						MilkShared s = new MilkShared(superContext);
						s.setSengriDate(dateCount);
						
						setResult(Activity.RESULT_OK);
					}
				}, dateCount);
				
				dialog.show();
			}
		});
		
		termView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkCountDialog dialog = new MilkCountDialog(superContext, "생리주기",
						new CountCallBack()
				{
					@Override
					public void getCount(int count)
					{
						termCount = count;
						termTextView.setText(termCount + "일");
						
						MilkShared s = new MilkShared(superContext);
						s.setSengriTerm(termCount);
						
						setResult(Activity.RESULT_OK);
					}
				}, termCount);
				
				dialog.show();
			}
		});
		
		firstView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkDayDialog dayDialog = new MilkDayDialog(superContext, new DateCallBank()
				{
					@Override
					public void getDate(int setYear, int setMonth, int setDay)
					{
						year = setYear;
						month = setMonth;
						day = setDay;
						firstTextView.setText(MilkUtil.getDateCom(year, month+1, day));
					}
				}, year, month, day);
				
				dayDialog.show();
			}
		});
		
		averageView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkShared s = new MilkShared(superContext);
				
				if (sengriType == 0)
				{
					s.setSengriType(1);
					averageTextView.setText("분석");
				}
				else
				{
					s.setSengriType(0);
					averageTextView.setText("일반");
				}
				
				sengriType = s.getSengriType();
				
				setResult(Activity.RESULT_OK);
			}
		});
		
		startTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkShared s = new MilkShared(superContext);
				s.setFirst(1);
				
				setInsertData();
				
				SengriCheck sc = new SengriCheck(superContext, superDb);
				sc.setData();
				
				Intent intent = new Intent(superContext, MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		MilkShared s = new MilkShared(superContext);
		
		dateCount = s.getSengriDate();
		termCount = s.getSengriTerm();
		sengriType = s.getSengriType();
		
		if (flag == FIRST)
		{
			titleTextView.setText("초기 세팅");
			firstView.setVisibility(View.VISIBLE);
			averageView.setVisibility(View.GONE);
			startTextView.setVisibility(View.VISIBLE);
			
			Calendar cal = Calendar.getInstance();
			year = cal.get(Calendar.YEAR);
			month = cal.get(Calendar.MONTH);
			day = cal.get(Calendar.DAY_OF_MONTH);
			firstTextView.setText(MilkUtil.getDateCom(year, month+1, day));
		}
		else
		{
			titleTextView.setText("생리 설정");
			firstView.setVisibility(View.GONE);
			averageView.setVisibility(View.VISIBLE);
			startTextView.setVisibility(View.GONE);
			
			if (sengriType == 0)
			{
				averageTextView.setText("일반");
			}
			else
			{
				averageTextView.setText("분석");
			}
		}
		
		dateTextView.setText(dateCount + "일");
		termTextView.setText(termCount + "일");
	}
	
	private boolean setInsertData()
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			MilkShared s = new MilkShared(superContext);
			
			map.put("year", year + "");
			map.put("month", month + "");
			map.put("day", day + "");
			map.put("term", s.getSengriDate() + "");
			map.put("date", MilkUtil.getDateDB(year, month, day));
			
			query.setPeriodDate(map);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
}
